package by.parfen.game.atrash.service.etc.impl;

import java.util.ArrayList;
import java.util.List;

import by.parfen.game.atrash.service.etc.GamePortalService;
import by.parfen.game.datamodel.enums.GameState;
import by.parfen.game.datamodel.etc.GamePortal;
import by.parfen.game.datamodel.etc.GameSession;

public class GamePortalServiceImpl implements GamePortalService {

	@Override
	public GamePortal create(String name) {
		return new GamePortal(name);
	}

	public List<GameSession> getSessions(GamePortal gamePortal, GameState gameState) {
		List<GameSession> res = new ArrayList<GameSession>();
		List<GameSession> gameSessions = gamePortal.getGameSessions();
		for (GameSession gameSession : gameSessions) {
			if (gameState == null || gameSession.getGameState() == gameState) {
				res.add(gameSession);
			}
		}
		return res;
	}

	@Override
	public List<GameSession> getNewSessions(GamePortal gamePortal) {
		return getSessions(gamePortal, GameState.START);
	}

	@Override
	public List<GameSession> getStartedSessions(GamePortal gamePortal) {
		return getSessions(gamePortal, GameState.PLAY);
	}

	@Override
	public List<GameSession> getFinishedSessions(GamePortal gamePortal) {
		return getSessions(gamePortal, GameState.FINISH);
	}

	@Override
	public List<GameSession> getAllSessions(GamePortal gamePortal) {
		return getSessions(gamePortal, null);
	}

}
